package com.flyou.ccnucenter.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.flyou.ccnucenter.R;
import com.flyou.ccnucenter.domain.AnonymousLove;
import com.flyou.ccnucenter.widget.heartLayout.HeartLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.GetListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * ============================================================
 * 项目名称：HenuCenterPro
 * 包名称：com.flyou.henucenter.adapter
 * 文件名：NoteRecycleViewAdapter
 * 类描述：
 * 创建人：flyou
 * 邮箱：fangjaylong@gmail.com
 * 创建时间：2015/12/26 15:03
 * 修改备注：
 * 版本：@version  V1.0
 * ============================================================
 **/
public class LoveRecycleViewAdapter extends RecyclerView.Adapter<LoveRecycleViewAdapter.MyViewHolder> {
    private Random mRandom = new Random();
    private Context context;
    private List<AnonymousLove> loves;
    private  List<Integer> headViewImageId;

    public interface OnItemClickLitener
    {
        void onItemClick(View view, int position);
        void onItemLongClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    public LoveRecycleViewAdapter(Context context, List<AnonymousLove> loves) {
        headViewImageId=new ArrayList<>();
        headViewImageId.add(R.drawable.man1);
        headViewImageId.add(R.drawable.man2);
        headViewImageId.add(R.drawable.man3);
        headViewImageId.add(R.drawable.man4);
        headViewImageId.add(R.drawable.woman1);
        headViewImageId.add(R.drawable.woman2);
        headViewImageId.add(R.drawable.woman3);
        headViewImageId.add(R.drawable.woman4);
        this.context=context;
        this.loves=loves;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder viewHolder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.view_love_list_item, null));

        return viewHolder;
    }
    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.nickNmae.setText("To: "+loves.get(position).getLoveName());
        holder.time.setText(loves.get(position).getUpdatedAt());
        holder.content.setText(loves.get(position).getContent());
        holder.headView.setImageResource(headViewImageId.get(loves.get(position).getHeadImageId()));
        holder.loveNumber.setText(loves.get(position).getLoveNumber()+"");
        // 如果设置了回调，则设置点击事件
        if (mOnItemClickLitener != null)
        {
            holder.itemView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    int pos = holder.getLayoutPosition();


                    AnonymousLove love = loves.get(pos);
                    BmobQuery<AnonymousLove> query = new BmobQuery<AnonymousLove>();
                    query.getObject(context, love.getObjectId(), new GetListener<AnonymousLove>() {
                        @Override
                        public void onSuccess(final AnonymousLove anonymousLove) {
                            AnonymousLove updateLove = new AnonymousLove();
                            anonymousLove.setLoveNumber(anonymousLove.getLoveNumber() + 1);
                            anonymousLove.update(context, anonymousLove.getObjectId(), new UpdateListener() {

                                @Override
                                public void onSuccess() {
                                    holder.loveNumber.setText((anonymousLove.getLoveNumber() + 1)+"");
                                }

                                @Override
                                public void onFailure(int code, String msg) {

                                }
                            });
                        }

                        @Override
                        public void onFailure(int i, String s) {

                        }
                    });
                    holder.hertlayout.addHeart(randomColor());
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener()
            {
                @Override
                public boolean onLongClick(View v)
                {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                    return false;
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return loves.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView nickNmae;
        TextView time;
        TextView content;
        TextView loveNumber;
        ImageView headView;
        HeartLayout hertlayout;


        public MyViewHolder(View view) {
            super(view);
            nickNmae = (TextView) view.findViewById(R.id.tv_love_nickName);
            time = (TextView) view.findViewById(R.id.tv_love_createTime);
            content = (TextView) view.findViewById(R.id.tv_love_content);
            loveNumber = (TextView) view.findViewById(R.id.love_number);
            headView = (ImageView) view.findViewById(R.id.iv_love_headview);
            hertlayout = (HeartLayout) view.findViewById(R.id.heart_layout);



        }
    }
    public void addItem(AnonymousLove love) {

        loves.add(love);
        notifyItemInserted(0);
    }

    public void removeItem(int position) {
        loves.remove(position);
        notifyItemRemoved(position);
    }
    private int randomColor() {
        return Color.rgb(mRandom.nextInt(255), mRandom.nextInt(255), mRandom.nextInt(255));
    }
}
